package com.neuedu.my12306.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.neuedu.my12306.entity.UserInfo;
import com.neuedu.my12306.service.IUserInfoService;
import com.neuedu.my12306.service.impl.IUserInfoServiceImpl;

/**
 * Servlet implementation class CheckUserNameServlet
 */
@WebServlet("/CheckUserNameServlet")
public class CheckUserNameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private IUserInfoService userInfoService;   
    public CheckUserNameServlet() {
        super();
        userInfoService = new IUserInfoServiceImpl();
        
        
    }


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userString = request.getParameter("userName");
		boolean exists = false;
		if (userString!=null) {
			UserInfo user = userInfoService.findUserInfoByid(userString);
			if (user!=null) {
				exists = true;
			}
		}
		Result result2=null;
		String result = "";
		if (exists) {
			result2 = new Result(true, "账号已经存在！");
			result = JSON.toJSONString(result2);
		}else {
			result2 = new Result(false, "账号可以使用");
			result = JSON.toJSONString(result2);
					
		}
		//使用fastjon把java对象转换为json字符串
		result = JSON.toJSONString(result2);
		System.out.println("查询结果：  "+result);
		//返回数据类型给浏览器，数据时json格式的数据。是json字符串
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().write(result);
	}
	class Result{
		private boolean result;
		private String msg;
		public Result() {
			super();
		}
		public Result(boolean result, String msg) {
			super();
			this.result = result;
			this.msg = msg;
		}
		public boolean isResult() {
			return result;
		}
		public void setResult(boolean result) {
			this.result = result;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
	}


}
